package com.hst.web.base.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hst.core.ServiceContext;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.web.auth.po.User;
import com.hst.web.base.entity.TDeptEmployee;
import com.hst.web.common.utils.ConstantUtil;
import com.hst.web.sys.entity.TRole;
import com.hst.web.sys.entity.TUserContractor;
import com.hst.web.sys.entity.TUserDept;
import com.hst.web.sys.entity.TUserRole;

/**
 * @author HP-P6
 *
 */
@Service
public class UserRoleService {

	/**
	 * 承包商
	 */
	public static final String USERTYPE_CONTRACTOR = "1";

	/**
	 * 企业
	 */
	public static final String USERTYPE_COMPANY = "2";
 
	/**
	 * 公安
	 */
	public static final String USERTYPE_POLICE = "9";

	private static IORDao dao;

	/**
	 * 企业员工检索关联到的员工code
	 * 
	 * @param map
	 * @return
	 */
	public static Map<String, ?> addCompanyEmpParam(Map<String, Object> map) {
		User u = (User) ServiceContext.getInstance().getUser();
		switch (u.getUserType()) {
		case USERTYPE_POLICE:
			break;
		case USERTYPE_COMPANY:
			List<String> list2 = new ArrayList<String>();
			if (u.getPrivileges().contains(ConstantUtil.COMPANY_MANAGER_PRIVLIGE)) {//企业管理员

			} else {
				for (TDeptEmployee d : dao.list(TDeptEmployee.class, addDeptParam(new ArrayList<ORQuery>()))) {
					if (!list2.contains(d.getDeptCode()))
						list2.add(d.getDeptCode());
				}
				list2.add("");
			}
			if (list2.size() > 0) {
				map.put("user_depts", list2);
				map.put("user_deptcode", list2);
			}
			map.put("user_companycode", u.getCompanyCode());
			break;
		}
		return map;
	}

	/**
	 * 企业员工检索关联到的员工code
	 * 
	 * @param query
	 * @return
	 */
	public static List<ORQuery> addCompanyEmpParam(List<ORQuery> query) {
		//
		User u = (User) ServiceContext.getInstance().getUser();
		switch (u.getUserType()) {
		case USERTYPE_POLICE:
			break;
		case USERTYPE_COMPANY:
			List<String> list2 = new ArrayList<String>();
			if (u.getPrivileges().contains(ConstantUtil.COMPANY_MANAGER_PRIVLIGE)) {// 企业管理员
			} else {
				for (TDeptEmployee d : dao.list(TDeptEmployee.class, addDeptParam(new ArrayList<ORQuery>()))) {
					if (!list2.contains(d.getDeptCode()))
						list2.add(d.getDeptCode());
				}
				list2.add("");
			}
			if (list2.size() > 0)
				query.add(new ORQuery(Op.in, "deptCode", list2));
			break;
		}
		return query;
	}

	/**
	 * 承包商员工检索关联到的员工code
	 * 
	 * @param map
	 * @return
	 */
	public static List<ORQuery> addContractorEmpParam(List<ORQuery> query) {
		User u = (User) ServiceContext.getInstance().getUser();
		switch (u.getUserType()) {
		case USERTYPE_POLICE:
			break;
		case USERTYPE_COMPANY:
			String companyCode = u.getCompanyCode();
			query.add(new ORQuery(Op.eq, "companyCode", companyCode));
			break;
		case USERTYPE_CONTRACTOR:
			List<String> listContractor = new ArrayList<String>();
			listContractor.add("");

			List<ORQuery> q = new ArrayList<ORQuery>();
			q.add(new ORQuery(Op.eq, "userid", u.getId()));
			if (!StringUtils.isEmpty(u.getCompanyCode()))
				q.add(new ORQuery(Op.eq, "companyCode", u.getCompanyCode()));
			for (TUserContractor e : dao.list(TUserContractor.class, q))
				listContractor.add(e.getContractorCode());

			query.add(new ORQuery(Op.in, "contractorCode", listContractor));
			query.add(new ORQuery(Op.eq, "companyCode", u.getCompanyCode()));
			break;
		}
		return query;
	}

	/**
	 * 承包商员工检索关联到的员工code
	 * 
	 * @param map
	 * @return
	 */
	public static Map<String, ?> addContractorEmpParam(Map<String, Object> map) {
		User u = (User) ServiceContext.getInstance().getUser();
		switch (u.getUserType()) {
		case USERTYPE_POLICE:
			break;
		case USERTYPE_COMPANY:
			String companyCode = u.getCompanyCode();
			map.put("user_companycode", companyCode);
			break;
		case USERTYPE_CONTRACTOR:
			List<String> listContractor = new ArrayList<String>();
			listContractor.add("");
			List<ORQuery> q = new ArrayList<ORQuery>();
			q.add(new ORQuery(Op.eq, "userid", u.getId()));
			if (!StringUtils.isEmpty(u.getCompanyCode()))
				q.add(new ORQuery(Op.eq, "companyCode", u.getCompanyCode()));
			for (TUserContractor e : dao.list(TUserContractor.class, q))
				listContractor.add(e.getContractorCode());
			map.put("user_contractorcode", listContractor);
			map.put("user_companycode", u.getCompanyCode());
			break;
		}
		return map;
	}

	//
	//
	//
	//

	public static List<ORQuery> addCompanyParam(List<ORQuery> query) {
		User u = (User) ServiceContext.getInstance().getUser();
		query.add(new ORQuery(Op.eq, "companyCode", u.getCompanyCode()));
		return query;
	}

	public static Map<String, ?> addCompanyParam(Map<String, Object> map) {
		User u = (User) ServiceContext.getInstance().getUser();
		map.put("user_companycode", u.getCompanyCode());
		return map;
	}

	public static List<ORQuery> addDeptParam(List<ORQuery> query) {
		User u = (User) ServiceContext.getInstance().getUser();
		List<String> listDept = new ArrayList<String>();
		listDept.add("");
		for (TUserDept e : dao.list(TUserDept.class, new ORQuery(Op.eq, "userid", u.getId())))
			listDept.add(e.getDeptCode());
		query.add(new ORQuery(Op.in, "deptCode", listDept));
		return query;
	}

	public static Map<String, ?> addDeptParam(Map<String, Object> map) {
		User u = (User) ServiceContext.getInstance().getUser();
		List<String> listDept = new ArrayList<String>();
		listDept.add("");
		for (TUserDept e : dao.list(TUserDept.class, new ORQuery(Op.eq, "userid", u.getId())))
			listDept.add(e.getDeptCode());
		map.put("user_deptcode", listDept);
		return map;
	}

	public TRole userRole(String userId) {
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq, "userid", userId));
		List<TUserRole> rels = dao.list(TUserRole.class, querys);
		if (rels.size() > 0) {
			String roleId = rels.get(0).getRoleid();
			TRole role = dao.get(TRole.class, roleId);
			return role;
		} else {
			return null;
		}
	}

	/**
	 * 用户是否存在相应角色
	 * 
	 * @param userId	用户id
	 * @param roleID	所需roleID
	 * @return
	 */
	public boolean isExistRole(String userId, String roleID) {
		boolean isExistRole = false;
		if (StringUtils.isEmpty(userId)) return isExistRole;
		if (StringUtils.isEmpty(roleID)) return isExistRole;
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq, "userid", userId));
		querys.add(new ORQuery(Op.eq, "roleid", roleID));
		List<TUserRole> rels = dao.list(TUserRole.class, querys);
		if (rels.size() > 0) {
			isExistRole = true;
		}
		return isExistRole;
	}	
	
	/**
	 * 取得用户所有角色
	 * 
	 * @param userId	用户id
	 * @param 
	 * @return List<TRole> 用户角色list
	 */	
	public List<TRole> getUserRole(String userId) {
		List<TRole> userRoleList = new ArrayList<TRole>();
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq, "userid", userId));
		List<TUserRole> rels = dao.list(TUserRole.class, querys);
		for (TUserRole userRole : rels) {
			String roleId = userRole.getRoleid();
			TRole role = dao.get(TRole.class, roleId);
			userRoleList.add(role);
		}
		return userRoleList;
	}
	
	@Autowired
	public void setDao(IORDao dao) {
		UserRoleService.dao = dao;
	}

}
